package com.dme.modules.web01.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dme.modules.entity.TransactionOne;
import com.dme.modules.service.transaction.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 代蒙恩
 * @Department 流程报表开发组
 * @Date 2022/6/24 22:52
 */
@RestController
@RequestMapping(path = "transaction")
public class TransactionController {

    private TransactionService transactionService;

    @Autowired
    public void setTransactionService(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @RequestMapping(path = "test")
    public String test(){
        System.out.println("test.............");
        return "success";
    }


    @RequestMapping(path = "initData")
    public String initData(){
        List<TransactionOne> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            TransactionOne transactionOne = new TransactionOne();
            transactionOne.setId(IdWorker.getId()).setName("张三 - "+i).setValue("value").setShowOrder(i);
            list.add(transactionOne);
        }
        transactionService.saveBatch(list);
        return "success";
    }

    @RequestMapping("delete")
    @Transactional(rollbackFor = Exception.class)
    public String A() throws Exception {

        B();
        C();

        return "success";
    }


    public void B() throws Exception {
        System.out.println("测试-事务");

        transactionService.removeById("1539393513600253969");

        try {
            int i = 1 / 0;
        }catch (Exception e){
            throw new Exception();
        }

        System.out.println(".......");
    }

    public void C(){
        System.out.println("测试-事务");
    }


}
